﻿using AnlianCommon.Common.Domains.BankCustomers;
using AnlianCommon.Common.DTO;
using AnlianCommon.Common.Infrastructure.IRepository;
using AnlianCommon.Enum;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace SheepCommon.API.Vendors
{
    public class BankaccountUpdateHandler : IRequestHandler<BankaccountUpdateCommand, Result>
    {
        private readonly IBaseRepository<Bankaccount> _bankaccountRepository;

        public BankaccountUpdateHandler(IBaseRepository<Bankaccount> bankaccountRepository)
        {
            _bankaccountRepository = bankaccountRepository;
        }
        /// <summary>
        /// 银行账户信息编辑业务逻辑
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result> Handle(BankaccountUpdateCommand request, CancellationToken cancellationToken)
        {
            var result = new Result();
            try
            {
                var bankaccountUpdate = await (await _bankaccountRepository.GetAll()).Where(c => c.BankaccountId == request.BankaccountId).FirstOrDefaultAsync();
                if (bankaccountUpdate != null)
                {
                    bankaccountUpdate.UpdatedOwnerId = "张三";
                    bankaccountUpdate.UpdatedTime = DateTime.Now;
                    var res = await _bankaccountRepository.Update(bankaccountUpdate);
                    if (res > 0)
                    {
                        result.Code = (int)CommonErrorCode.Success;
                        result.Msg = "修改成功";
                    }
                    else
                    {
                        result.Msg = "修改失败";
                    }
                }
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
    }
}
